package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.SettAccount;
import pers.cj.pdos.service.ISettAccountService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.vo.SettAccountVo;
import pers.cj.pdos.vo.UserVo;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/settAccount")
public class SettAccountController extends BaseController {
    @Autowired
    private ISettAccountService settAccountService;

    @RequiresPermissions("settAccount:query")
    @RequestMapping("/settAccount")
    public String settAccount(){
        return "settAccount/settAccount";
    }


    @RequiresPermissions("settAccount:insert")
    @RequestMapping("/settAccountNew")
    public String settAccountNew(){
        return "settAccount/settAccountNew";
    }

    @RequiresPermissions("settAccount:insert")
    @RequestMapping("/settAccountEdit")
    public String settAccountEdit(Model model,String id){
        model.addAttribute("id",id);
        return "settAccount/settAccountEdit";
    }

    @ResponseBody
    @RequiresPermissions("settAccount:query")
    @RequestMapping("/settAccount/getSettAccounts")
    public ResponseList getSettAccounts(int page, int limit, String searchContent) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();

        List<SettAccountVo> list = settAccountService.listVoPage(page, limit,company.getId(), searchContent, PdosUtils.getViewOtherStoreId(userVo,null));
        //返回数据
        return ResponseList.success(settAccountService.count(company.getId(),searchContent, PdosUtils.getViewOtherStoreId(userVo,null)), list);
    }

    @ResponseBody
    @RequiresPermissions("settAccount:insert")
    @RequestMapping("/settAccount/add")
    public ResponseEntity add(@RequestBody SettAccount settAccount) {
        Company company = getCompany();
        if (settAccountService.checkName(settAccount.getName(),company.getId())){
            return ResponseEntity.fail("账户名称已存在");
        }
        settAccount.setBalance(settAccount.getInitBalance());
        DataUtil.processEntity(company, settAccount);
        settAccountService.insert(settAccount);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("unit:delete")
    @RequestMapping("/settAccount/delete")
    public ResponseEntity delete(@RequestBody SettAccount settAccount) {
        try {
            DataUtil.processEntity(false, getCompany(), settAccount);
            settAccountService.remove(settAccount);
            return ResponseEntity.success();
        } catch (Exception e) {
            return ResponseEntity.fail("已被使用，删除失败");
        }
    }

    @ResponseBody
    @RequiresPermissions("settAccount:insert")
    @RequestMapping("/settAccount/update")
    public ResponseData update(@RequestBody SettAccount settAccount) {
        Company company = getCompany();
        SettAccount temp = settAccountService.getById(settAccount.getId());
        if (temp==null){
            ResponseData.fail("id不正确");
        }

        if (!temp.getName().equals(settAccount.getName()) && settAccountService.checkName(settAccount.getName(),company.getId())){
            return ResponseData.fail("账户名称已存在");
        }
        DataUtil.processEntity(false, company, settAccount);
        settAccountService.update(settAccount);

        return ResponseData.success(settAccountService.getVoById(settAccount.getId()));
    }

    @ResponseBody
    @RequestMapping("/settAccount/listVo")
    public ResponseData listVo(){
        List<SettAccountVo> settAccountVoList = settAccountService.listVo(getCompany().getId(), null);
        return ResponseData.success(settAccountVoList);
    }

    @ResponseBody
    @RequestMapping("/settAccount/getByStore")
    public ResponseData getByStore(String storeId){
        List<SettAccount> settAccountList = settAccountService.getByStore(storeId,getCompany().getId());
        return ResponseData.success(settAccountList);
    }

    @ResponseBody
    @RequestMapping("/settAccount/getVoById")
    public ResponseData getVoById(String id){
        SettAccountVo vo = settAccountService.getVoById(id);
        if (vo==null){
            ResponseData.fail("id不正确");
        }
        return ResponseData.success(vo);
    }
}
